{include file="common/header"}
<body class="pear-container">
<div class="layui-row layui-col-space10">
    <div class="layui-col-md8">
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form top-search-from" action="">
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">问题分类</label>
                        <div class="layui-input-block">
                            <select name="cid">
                                <option value="">请选择问题分类</option>
                                {foreach $categoryList as $k=>$v}
                                <option value="{$v.id}">{$v.title}</option>
                                {/foreach}
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="query">
                            <i class="layui-icon layui-icon-search"></i>查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md">
                            <i class="layui-icon layui-icon-refresh"></i>重置
                        </button>
                    </div>
                    <div class="toggle-btn">
                        <a class="layui-hide">展开<i class="layui-icon layui-icon-down"></i></a>
                        <a class="layui-hide">收起<i class="layui-icon layui-icon-up"></i></a>
                    </div>
                </form>
            </div>
        </div>
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="questions-list-table" lay-filter="questions-list-table"></table>
            </div>
        </div>
    </div>
    <div class="layui-col-md4">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="empty">
                    <fieldset class="layui-elem-field layui-field-title">
                        <legend>答案列表</legend>
                    </fieldset>
                    <svg class="empty" style="width: 100%;" width="184" height="152" viewBox="0 0 184 152"
                         xmlns="http://www.w3.org/2000/svg">
                        <g fill="none" fillrule="evenodd">
                            <g transform="translate(24 31.67)">
                                <ellipse fillopacity=".8" fill="#F5F5F7" cx="67.797" cy="106.89" rx="67.797"
                                         ry="12.668"></ellipse>
                                <path d="M122.034 69.674L98.109 40.229c-1.148-1.386-2.826-2.225-4.593-2.225h-51.44c-1.766 0-3.444.839-4.592 2.225L13.56 69.674v15.383h108.475V69.674z"
                                      fill="#AEB8C2"></path>
                                <path d="M101.537 86.214L80.63 61.102c-1.001-1.207-2.507-1.867-4.048-1.867H31.724c-1.54 0-3.047.66-4.048 1.867L6.769 86.214v13.792h94.768V86.214z"
                                      fill="url(#linearGradient-1)" transform="translate(13.56)"></path>
                                <path d="M33.83 0h67.933a4 4 0 0 1 4 4v93.344a4 4 0 0 1-4 4H33.83a4 4 0 0 1-4-4V4a4 4 0 0 1 4-4z"
                                      fill="#F5F5F7"></path>
                                <path d="M42.678 9.953h50.237a2 2 0 0 1 2 2V36.91a2 2 0 0 1-2 2H42.678a2 2 0 0 1-2-2V11.953a2 2 0 0 1 2-2zM42.94 49.767h49.713a2.262 2.262 0 1 1 0 4.524H42.94a2.262 2.262 0 0 1 0-4.524zM42.94 61.53h49.713a2.262 2.262 0 1 1 0 4.525H42.94a2.262 2.262 0 0 1 0-4.525zM121.813 105.032c-.775 3.071-3.497 5.36-6.735 5.36H20.515c-3.238 0-5.96-2.29-6.734-5.36a7.309 7.309 0 0 1-.222-1.79V69.675h26.318c2.907 0 5.25 2.448 5.25 5.42v.04c0 2.971 2.37 5.37 5.277 5.37h34.785c2.907 0 5.277-2.421 5.277-5.393V75.1c0-2.972 2.343-5.426 5.25-5.426h26.318v33.569c0 .617-.077 1.216-.221 1.789z"
                                      fill="#DCE0E6"></path>
                            </g>
                            <path d="M149.121 33.292l-6.83 2.65a1 1 0 0 1-1.317-1.23l1.937-6.207c-2.589-2.944-4.109-6.534-4.109-10.408C138.802 8.102 148.92 0 161.402 0 173.881 0 184 8.102 184 18.097c0 9.995-10.118 18.097-22.599 18.097-4.528 0-8.744-1.066-12.28-2.902z"
                                  fill="#DCE0E6"></path>
                            <g transform="translate(149.65 15.383)" fill="#FFF">
                                <ellipse cx="20.654" cy="3.167" rx="2.849" ry="2.815"></ellipse>
                                <path d="M5.698 5.63H0L2.898.704zM9.259.704h4.985V5.63H9.259z"></path>
                            </g>
                        </g>
                    </svg>
                </div>
                <table id="questions-answers-table" lay-filter="questions-answers-table"></table>
            </div>
        </div>
    </div>
</div>

</body>
<script type="text/html" id="questions-list-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="addQuestion">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>

<script type="text/html" id="questions-list-bar">
    <button class="pear-btn pear-btn-primary pear-btn-xs" lay-event="editQuestion">
        <i class="layui-icon">编辑</i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-xs" lay-event="removeQuestion">
        <i class="layui-icon">删除</i>
    </button>
</script>

<script type="text/html" id="questions-answers-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="addAnswer">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>
<script type="text/html" id="questions-answers-bar">
    <button class="pear-btn pear-btn-primary pear-btn-xs" lay-event="editAnswer">
        <i class="layui-icon">编辑</i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-xs" lay-event="removeAnswer">
        <i class="layui-icon">删除</i>
    </button>
</script>
<script type="text/html" id="status">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用"
           lay-filter="changeStatus"
           {{# if(d.status==1){ }} checked {{# } }}>
</script>
{include file="common/footer"}
<script>
    layui.use(['table', 'form', 'jquery', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        let questionId = 0;

        let MODULE_PATH = "{$Request.root}/Questions/";
        table.render({
            elem: '#questions-list-table',
            url: MODULE_PATH + 'list',
            cols: [
                [
                    {title: 'ID', field: 'id', width: 80, align: 'center'},
                    {title: '问题分类', field: 'category_title', align: 'center'},
                    {
                        title: '问题类型', field: 'type_title', align: 'center', templet: function (d) {
                            return '<span class="layui-badge ' + d.type_class + '">' + d.type_title + '</span>';
                        }
                    },
                    {title: '问题题目', field: 'title', align: 'center'},
                    {
                        title: '问题难度', field: 'difficulty', align: 'center', templet: function (d) {
                            return d.difficulty + '星';
                        }
                    },
                    {title: '问题状态', field: 'status', align: 'center', templet: '#status'},
                    {title: '操作', toolbar: '#questions-list-bar', align: 'center', width: 135}
                ]
            ],
            cellMinWidth: 100,
            page: true,
            skin: 'grid',
            even: 'true',
            size: 'lg',
            height: 'full-148',
            toolbar: '#questions-list-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter']
        });

        window.renderAnswer = function (qid) {
            questionId = qid;
            $(".empty").hide();
            table.render({
                elem: '#questions-answers-table',
                url: MODULE_PATH + 'answers/questionId/' + qid,
                cols: [
                    [
                        {title: '问题选项', field: 'key', width: 80, align: 'center'},
                        {title: '问题答案', field: 'title', align: 'center'},
                        {
                            title: '正确答案', field: 'correct', width: 80, align: 'center', templet: function (d) {
                                if (d.correct) return '<span class="layui-badge layui-bg-blue">是</span>';
                                else return '<span class="layui-badge">否</span>';
                            }
                        },
                        {title: '操作', toolbar: '#questions-answers-bar', align: 'center', width: 135}
                    ]
                ],
                cellMinWidth: 100,
                page: true,
                skin: 'grid',
                even: 'true',
                size: 'lg',
                toolbar: '#questions-answers-toolbar',
                defaultToolbar: [{
                    title: '刷新',
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter']
            });
        }

        table.on('tool(questions-list-table)', function (obj) {
            if (obj.event === 'removeQuestion') {
                window.removeQuestion(obj);
            } else if (obj.event === 'editQuestion') {
                window.editQuestion(obj.data['id'])
            }
        });

        table.on('toolbar(questions-list-table)', function (obj) {
            if (obj.event === 'addQuestion') {
                window.editQuestion(0);
            } else if (obj.event === 'refresh') {
                window.refreshQuestion();
            }
        });

        table.on('row(questions-list-table)', function (obj) {
            window.renderAnswer(obj.data['id'])
        });

        form.on('submit(query)', function (data) {
            table.reload('questions-list-table', {
                where: data.field,
                page: {curr: 1}
            })
            return false;
        });

        window.editQuestion = function (id) {
            layer.open({
                type: 2,
                maxmin: true,
                title: id === 0 ? '添加问题' : '编辑问题',
                shade: 0.1,
                area: common.screenSmall(),
                content: MODULE_PATH + 'editQuestion/id/' + id
            });
        }

        window.refreshQuestion = function () {
            table.reload('questions-list-table');
        }

        window.removeQuestion = function (obj) {
            layer.confirm('确定要删除该数据?', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + 'goQuestionRemove',
                    data: {id: obj.data.id},
                    dataType: 'json',
                    type: 'POST',
                    success: function (res) {
                        layer.close(loading);
                        if (res.code == 200) {
                            layer.msg(res.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                obj.del();
                                window.refreshAnswer()
                            });
                        } else {
                            layer.msg(res.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        form.on('switch(changeStatus)', function (data) {
            var status = data.elem.checked ? 1 : 0;
            var id = this.value;
            var load = layer.load();
            $.post('goQuestionStatus', {status: status, id: id}, function (res) {
                layer.close(load);
                //判断有没有权限
                if (res && res.code == 999) {
                    layer.msg(res.msg, {
                        icon: 5,
                        time: 1500,
                    }, function () {
                        window.refresh();
                    })
                    return false;
                } else if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1, time: 1500})
                } else {
                    layer.msg(res.msg, {icon: 2, time: 1500}, function () {
                        $(data.elem).prop('checked', !$(data.elem).prop('checked'));
                        form.render()
                    })
                }
            })
        });

        table.on('tool(questions-answers-table)', function (obj) {
            if (obj.event === 'removeAnswer') {
                window.removeAnswer(obj);
            } else if (obj.event === 'editAnswer') {
                window.editAnswer(obj.data['id']);
            }
        });

        table.on('toolbar(questions-answers-table)', function (obj) {
            if (obj.event === 'addAnswer') {
                window.editAnswer(0);
            } else if (obj.event === 'refresh') {
                window.refreshAnswer();
            }
        });

        window.editAnswer = function (id) {
            layer.open({
                type: 2,
                maxmin: true,
                title: id === 0 ? '添加答案' : '编辑答案',
                shade: 0.1,
                area: common.screenSmall(),
                content: MODULE_PATH + 'editAnswer/qid/' + questionId + '/id/' + id
            });
        }

        window.removeAnswer = function (obj) {
            layer.confirm('确定要删除该数据?', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + 'goAnswerRemove',
                    data: {id: obj.data.id},
                    dataType: 'json',
                    type: 'POST',
                    success: function (res) {
                        layer.close(loading);
                        if (res.code == 200) {
                            layer.msg(res.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(res.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refreshAnswer = function () {
            table.reload('questions-answers-table');
        }
    })
</script>
</html>
